/*    */ package server;
/*    */ 
/*    */ import java.awt.Color;
/*    */ import java.io.OutputStream;
/*    */ import java.io.PrintStream;
/*    */ import javax.swing.JTextPane;
/*    */ import javax.swing.SwingUtilities;
/*    */ import javax.swing.text.BadLocationException;
/*    */ import javax.swing.text.Document;
/*    */ import javax.swing.text.SimpleAttributeSet;
/*    */ import javax.swing.text.StyleConstants;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class GUIPrintStream
/*    */   extends PrintStream
/*    */ {
/*    */   public static final int OUT = 0;
/*    */   public static final int ERR = 1;
/*    */   public static final int NOTICE = 2;
/*    */   public static final int PACKET = 3;
/*    */   private final JTextPane mainComponent;
/*    */   private final JTextPane component;
/*    */   private final int type;
/*    */   private final int lineLimit;
/*    */   
/*    */   public GUIPrintStream(OutputStream out, JTextPane mainComponent, JTextPane component, int type) {
/* 29 */     super(out);
/* 30 */     this.mainComponent = mainComponent;
/* 31 */     this.component = component;
/* 32 */     this.type = type;
/* 33 */     this.lineLimit = 100;
/*    */   }
/*    */   
/*    */   public GUIPrintStream(OutputStream out, JTextPane mainComponent, JTextPane component, int type, int lineLimit) {
/* 37 */     super(out);
/* 38 */     this.mainComponent = mainComponent;
/* 39 */     this.component = component;
/* 40 */     this.type = type;
/* 41 */     this.lineLimit = lineLimit;
/*    */   }
/*    */   
/*    */   public void write(byte[] buf, int off, int len) {
/*    */     Color col;
/* 46 */     super.write(buf, off, len);
/* 47 */     String message = new String(buf, off, len);
/*    */     
/* 49 */     switch (this.type) {
/*    */       case 0:
/* 51 */         col = Color.BLACK;
/*    */         break;
/*    */       case 1:
/* 54 */         col = Color.RED;
/*    */         break;
/*    */       case 2:
/* 57 */         col = Color.BLUE;
/*    */         break;
/*    */       case 3:
/* 60 */         col = Color.GRAY;
/*    */         break;
/*    */       default:
/* 63 */         col = Color.BLACK;
/*    */         break;
/*    */     } 
/* 66 */     SwingUtilities.invokeLater(() -> {
/*    */           SimpleAttributeSet attrSet = new SimpleAttributeSet();
/*    */           
/*    */           StyleConstants.setForeground(attrSet, col);
/*    */           
/*    */           Document doc = this.component.getDocument();
/*    */           Document docMain = this.mainComponent.getDocument();
/*    */           try {
/*    */             String[] docMainInfo = docMain.getText(0, docMain.getLength()).split("\r\n");
/*    */             String[] docInfo = doc.getText(0, doc.getLength()).split("\r\n");
/*    */             if (docMainInfo.length >= this.lineLimit + 1) {
/*    */               for (int i = 0; i <= docMainInfo.length - this.lineLimit - 1; i++) {
/*    */                 docMain.remove(0, docMainInfo[i].length() + 2);
/*    */               }
/*    */             }
/*    */             if (docInfo.length >= this.lineLimit + 1) {
/*    */               for (int i = 0; i <= docInfo.length - this.lineLimit - 1; i++) {
/*    */                 doc.remove(0, docInfo[i].length() + 2);
/*    */               }
/*    */             }
/*    */             docMain.insertString(docMain.getLength(), message, attrSet);
/*    */             doc.insertString(doc.getLength(), message, attrSet);
/* 88 */           } catch (BadLocationException e) {
/*    */             this.component.setText("輸出出錯:" + e + "\r\n內容:" + message + "\r\n類型:" + this.type);
/*    */           } 
/*    */         });
/*    */   }
/*    */ }


/* Location:              C:\PGX\Server.jar!\server\GUIPrintStream.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */